package com.yw.smartcityforjava.fragment;

import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.CombinedChart;
import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.data.RadarData;
import com.github.mikephil.charting.data.RadarDataSet;
import com.github.mikephil.charting.data.RadarEntry;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.yw.smartcityforjava.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class DataFragment extends Fragment {


    private LineChart lineChart;
    private BarChart barChart;

    public DataFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_news, container, false);
        TextView titleTv = root.findViewById(R.id.titleTv);
        titleTv.setText("数据分析");
//折线图
        lineChart = root.findViewById(R.id.lineChart);
        List<String> newsTitles = Arrays.asList("新闻1","新闻2","新闻3","新闻4","新闻5","新闻6","新闻7","新闻8");
        List<Entry> entryList = new ArrayList<>();
        for(int i=0;i<8;i++){
            Random random = new Random();
            entryList.add(new Entry(i,random.nextInt(100)));
        }
        LineDataSet lineDataSet = new LineDataSet(entryList, "点赞数量");
        lineDataSet.setColor(Color.RED);
        LineData lineData = new LineData(lineDataSet);
        lineChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        lineChart.getAxisRight().setEnabled(false);
        lineChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(newsTitles));
        lineChart.setData(lineData);

//条形图
        barChart = root.findViewById(R.id.barChart);
        List<BarEntry> barEntrys = new ArrayList<>();
        for(int i=0;i<8;i++){
            Random random = new Random();
            barEntrys.add(new BarEntry(i,random.nextInt(100)));
        }
        BarDataSet barDataSet = new BarDataSet(barEntrys, "评论数量");
        barDataSet.setColor(Color.YELLOW);
        BarData barData = new BarData(barDataSet);
        barChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        barChart.getAxisRight().setEnabled(false);
        barChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(newsTitles));
        barChart.setData(barData);
//横向条形图
        HorizontalBarChart hBarChart = root.findViewById(R.id.hBarChart);
        hBarChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        hBarChart.getAxisRight().setEnabled(false);
        hBarChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(newsTitles));
        hBarChart.setData(barData);
//结合图
        CombinedChart combinedChart =  root.findViewById(R.id.comChart);
        CombinedData combinedData = new CombinedData();
        combinedData.setData(lineData);
        combinedData.setData(barData);
        combinedChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        combinedChart.getAxisRight().setEnabled(false);
        combinedChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(newsTitles));

        combinedChart.setData(combinedData);

        PieChart pieChart = root.findViewById(R.id.pieChart);
        List<PieEntry> pieEntrys = new ArrayList<>();
        pieEntrys.add(new PieEntry(18.5f, "Green"));
        pieEntrys.add(new PieEntry(26.7f, "Yellow"));
        pieEntrys.add(new PieEntry(24.0f, "Red"));
        pieEntrys.add(new PieEntry(30.8f, "Blue"));
        PieDataSet pieDataSet = new PieDataSet(pieEntrys, "新闻访问");
        pieDataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        pieDataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        pieDataSet.setColors(Arrays.asList(Color.GREEN, Color.YELLOW, Color.RED, Color.BLUE));
        pieDataSet.setValueTextColor(Color.BLACK);
        PieData pieData = new PieData(pieDataSet);
        pieChart.setEntryLabelColor(Color.BLACK);
        pieChart.setData(pieData);

        RadarChart radarChart = root.findViewById(R.id.radarChart);
        List<RadarEntry> radarEntries = new ArrayList<>();
        radarEntries.add(new RadarEntry(1, "111"));
        radarEntries.add(new RadarEntry(2, "222"));
        radarEntries.add(new RadarEntry(3, "333"));
        radarEntries.add(new RadarEntry(4, "444"));
        radarEntries.add(new RadarEntry(5, "555"));
        RadarDataSet radarDataSet = new RadarDataSet(radarEntries, "新闻舆情");

        RadarData radarData = new RadarData(radarDataSet);

        radarChart.setData(radarData);
        return root;
    }
}